package NewCoder;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

public class BM46 {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> list=new ArrayList<>();
        if(input.length==0 || k==0) return list;
        PriorityQueue<Integer> q=new PriorityQueue<>((o1,o2)->o2.compareTo(o1));
        for(int i=0;i<input.length;i++){
            if(q.size()<k){
                q.offer(input[i]);
            }else{
                if(input[i]<q.peek()){
                    q.poll();
                    q.offer(input[i]);
                }
            }
        }
        for(int i=0;i<k;i++){
            list.add(q.poll());
        }
        return list;
    }
}
